<?php
/**
 * Enter description here...
 *
 * @package if
 * @subpackage module.helper.controllers
 */
class IfHelperModuleController extends IHelperController {
	function doIndex() {
		$this->modules = array();
		$modules = IApplication::singleton()->modules();
		foreach ($modules as $name => $dir) {
			$this->modules[$name] = array();
			$module = IModuleManager::singleton()->moduleByName($name);
			$this->modules[$name]["version"] = $module->version();
			$this->modules[$name]["name"] = $module->name() ? $module->name() : $name;
			$this->modules[$name]["author"] = $module->maintainers() ? $module->maintainers() : "";
			$this->modules[$name]["dir"] = realpath($dir);
		}

	}
	
	function doCreate() {
		if (!$this->form()->isValid()) {
			return;
		}
		$dir = x("dir");
		
		$config = array(
			"id" => trim(xn("id")) ? trim(xn("id")) : $dir,
			"name" => trim(xn("name")),
			"summary" => xn("summary"),
			"description" => xn("description"),
			"license" => xn("license"),
			"maintainers" => xn("maintainers"),
			"release" => array( 
				"version" => trim(xn("version")),
				"date" => trim(xn("date")) 
			),
			
			"installable" => xn("install") ? 1 : 0
		);
		
		//创建根目录
		$basedir = IF_APP_ROOT . "/lib/modules/{$dir}";
		$dir = new IFile($basedir);
		$dir->mkdirs();
		
		//写入配置文件 
		//_module.php
		$version = trim(xn("version"));
		$configContent = "<?php\n/**\n * @package if\n * @subpackage module.{$dir}\n * @since {$version} \n */\nreturn " . var_export($config, true) . ";\n?>";
		$file = new IFile($basedir . "/_module.php");
		$file->writer()->write($configContent);
		
		//_class.php
		$configContent = "<?php\n/**\n * @package if\n * @subpackage module.{$dir}\n * @since {$version} \n */\nreturn array(\n\t\"*\"\n);\n?>";
		$file = new IFile($basedir . "/_class.php");
		$file->writer()->write($configContent);
		
		//_config.php
		$configContent = "<?php\n/**\n * @package if\n * @subpackage module.{$dir}\n * @since {$version} \n */\nreturn array(\n\n);\n?>";
		$file = new IFile($basedir . "/_config.php");
		$file->writer()->write($configContent);
		
		//创建assets
		$assetsFile = new IFile($basedir . "/_assets");
		$assetsFile->mkdirs();
		
		//如果需要安装
		if (xn("install")) {
			//创建deploy
			$deployFile = new IFile($basedir . "/_deploy");
			$deployFile->mkdirs();
			
			//创建目录
			foreach (array("controllers", "helpers", "filters", "views/default/templates", "models") as $dir) {
				$deployFile = new IFile($basedir . "/_deploy/{$dir}");
				$deployFile->mkdirs();
			}
			
			//创建安装器
			$controllerContent = "<?php
/**
 * 安装器
 */
class _installController extends IModuleController {
	function doIndex() {
		//在这里写你的安装代码
		
		//在你的安装程序结束后，需要注册当前模块，否则无法结束安装
		\$this->register();
		
		//结束提示
		echo \"成功安装模块\" . \$this->module->name() . \"\";
	}
}

?>";
			$file = new IFile($basedir . "/_deploy/controllers/_install.php");
			$file->writer()->write($controllerContent);
		}
		
		//跳回
		if_clean_compiler_dir();
		$this->redirect("index");
	}
	
	function doDelete() {
		$module = IModuleManager::singleton()->moduleByName(x("name"));
		if ($module) {
			$dir = new IFile($module->dir());
			$dir->delete();
		}
		if_clean_compiler_dir();
		$this->redirect("index");
	}
}

?>